package com.bigstonecontainer.training.springboot.frm.server.sys.controller;

import com.bigstonecontainer.training.springboot.frm.server.core.domain.Result;
import com.bigstonecontainer.training.springboot.frm.server.core.service.AuthService;
import com.bigstonecontainer.training.springboot.frm.server.sys.domain.SysMenu;
import com.bigstonecontainer.training.springboot.frm.server.sys.service.SysMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * 菜单权限表控制器。
 *
 * @author jiaodacailei
 */
@RestController
@RequestMapping("/sys/menu")
public class SysMenuController {

    /**
     * 注入SysMenuService
     */
    @Autowired
    private SysMenuService sysMenuService;

    @Autowired
    private AuthService authService;

    @GetMapping("/currentlist")
    public Result<List<SysMenu>> getCurrentUserMenuList(@RequestHeader("Authorization") String token) {
        List<SysMenu> menuList = sysMenuService.getCurrentUserMenuList(token);
        return Result.success(menuList);
    }

    /**
     * 菜单权限表列表查询
     *
     * @return 菜单权限表列表
     */
    @PostMapping("/list")
    public Result<List<SysMenu>> getList() {
        List<SysMenu> list = sysMenuService.getList();
        return Result.success(list);
    }

    /**
     * 获取单个菜单权限表信息
     *
     * @param id 菜单权限表ID
     * @return 单个菜单权限表信息
     */
    @GetMapping("/{id}")
    public Result<SysMenu> getOne(@PathVariable("id") Long id) {
        SysMenu one = sysMenuService.getOne(id);
        return Result.success(one);
    }

    /**
     * 新建一个菜单权限表
     *
     * @param menu 菜单权限表
     * @return
     */
    @PostMapping
    public Result<Integer> create(@RequestBody SysMenu menu) {
        Integer result = sysMenuService.create(menu);
        return Result.success(result);
    }

    /**
     * 修改一个菜单权限表
     *
     * @param menu 菜单权限表
     * @return
     */
    @PutMapping
    public Result<Integer> updateById(@RequestBody SysMenu menu) {
        Integer result = sysMenuService.updateById(menu);
        return Result.success(result);
    }

    /**
     * 删除单个菜单权限表
     *
     * @param id 菜单权限表ID
     * @return
     */
    @DeleteMapping("/{id}")
    public Result<Integer> delete(@PathVariable("id") Long id) {
        Integer result = sysMenuService.deleteById(id);
        return Result.success(result);
    }


    /**
     * 获取菜单下拉树列表
     */
    @GetMapping("/treeselect")
    public Result<List<SysMenu>> treeselect(@RequestHeader("Authorization") String authorizationHeaders,SysMenu menu ) {
     Long userId=authService.getLoginUser(authorizationHeaders).getUserId();
        List<SysMenu> menus = sysMenuService.selectMenuList(menu,userId);
        return Result.success(menus);
    }

    /**
     * 加载对应角色菜单列表树
     */
    @GetMapping(value = "/roleMenuTreeselect/{roleId}")
    public Result roleMenuTreeselect( @RequestHeader("Authorization") String authorizationHeaders,@PathVariable("roleId") Long roleId) {
        Long userId=authService.getLoginUser(authorizationHeaders).getUserId();
        List<SysMenu> menus = sysMenuService.selectMenuList(userId);

        HashMap<String, Object> map = new HashMap<>();
        map.put("checkedKeys", sysMenuService.selectMenuListByRoleId(roleId));
        map.put("menus", sysMenuService.buildMenuTreeSelect(menus));
        return Result.success(map);
    }

}
